package edu.wtbu.cs.androiddemo.activity.jump;

import android.os.Bundle;

import android.view.MenuItem;


import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;


import com.google.android.material.bottomnavigation.BottomNavigationView;


import edu.wtbu.cs.androiddemo.R;

import edu.wtbu.cs.androiddemo.activity.jump.fragment.FragmentGouwuche;
import edu.wtbu.cs.androiddemo.activity.jump.fragment.FragmentMe;
import edu.wtbu.cs.androiddemo.activity.jump.fragment.FragmentShouye;


/**
 * 使用BottomNavigationView 完成tab切换
 *
 */
public class FragmentDemoActivity extends AppCompatActivity {

    BottomNavigationView bottonNavBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_fragment_demo);
        initBottomNav();
        //跳转后默认进入首页
        Fragment fragment=new FragmentShouye();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fragment_container, fragment).commit();
    }


    private void initBottomNav(){
        bottonNavBar = findViewById(R.id.bottom_nav);
        final FragmentManager fragmentManager = getSupportFragmentManager();
        bottonNavBar.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected( MenuItem item) {
                FragmentTransaction transaction = fragmentManager.beginTransaction();
                int tabId = item.getItemId();
                Fragment fragment=new FragmentShouye();
                if (tabId ==  R.id.bottom_home){
                    fragment =  new FragmentShouye();;
                }else if (tabId ==  R.id.bottom_info){
                    fragment = new FragmentGouwuche();
                }else {
                    fragment = new FragmentMe();
                }
                transaction.replace(R.id.fragment_container, fragment).commit();
                return true;
            }
        });
    }
}